#include "widget.h"
#include "ui_widget.h"

#include "data.h"
#include <ctime>
#include <QIcon>
#include <QPainter>
#include <QTimer>
#include <QLabel>


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{

    ui->setupUi(this);

    initScene();
    playGame();

}

Widget::~Widget()
{
    delete ui;
}

void Widget::initScene()
{
    setFixedSize(GAME_WIDTH,GAME_HEIGHT);//设置窗口大小

    setWindowTitle(GAME_TITLE);//游戏名字

    life = new QLabel(this);
    score = new QLabel(this);
    //设定标签样式
    life->setFont(QFont("Algerian",16));
    life->setStyleSheet(
    "QLabel{background:transparent;color:white;}");
    life->setGeometry(rect().x()+10, rect().y()+10,90, 30);

     score->setFont(QFont("Algerian",16));
     score->setStyleSheet(
    "QLabel{background:transparent;color:white;}");
     score->setGeometry(rect().x()+10, rect().y()+50,200, 30);


    m_Timer.setInterval(GAME_RATE);//定时器函数

    m_recorder = 0;//初始化敌机出场时间间隔

    srand((unsigned int)time(NULL));//随机数种子
}

void Widget::playGame()
{
    //启动定时器
    m_Timer.start();
    //定时器与地图刷新联系
    connect(&m_Timer,&QTimer::timeout,
            [=](){
        //敌机出现
        enemyToScene();
        //更新游戏中元素的坐标
        updatePosition();
        //碰撞检测
        collisionDetection();
        //重新绘制图片
        update();
    });
}

void Widget::updatePosition(){
    if(isover){
        return;
    }

    m_map.mapPosition();//屏幕的更新即地图的移动
    m_hero.shoot();
    for(int i = 0 ; i< ENEMY_NUM;i++)
    {
        //非空闲敌机 射击
        if(m_enemys[i].m_Free == false)
        {
            m_enemys[i].shoot();
        }
    }
    //计算子弹坐标
    for(int i = 0 ;i < BULLET_NUM;i++)
    {
        //如果子弹状态为非空闲，计算发射位置
        if(!m_hero.m_bullets[i].m_Free)
        {
            m_hero.m_bullets[i].updatePosition();
        }
    }
    //敌机坐标计算
    for(int i = 0 ; i< ENEMY_NUM;i++)
    {
        //非空闲敌机 更新坐标
        if(m_enemys[i].m_Free == false)
        {
            m_enemys[i].updatePosition();
        }
    }
    //计算敌机子弹坐标
    for(int i = 0 ; i< ENEMY_NUM;i++)
    {
        for(int j = 0 ; j< ENEMY_NUM;j++)
        {
            //如果子弹状态为非空闲，计算发射位置
            if(!m_enemys[i].m_bullets[j].m_Free)
            {
                m_enemys[i].m_bullets[j].updatePosition();
            }
        }

    }
    //计算爆炸播放的图片
    for(int i = 0 ; i < BOMB_NUM;i++)
    {
        if(m_bombs[i].m_Free == false)
        {
            m_bombs[i].updateInfo();
        }
    }

    life->setText(QString(LIFE).arg(m_hero.m_life));	//随时更新相关信息
    score->setText(QString(SCORE).arg(m_hero.m_score));

    if(m_hero.m_life<=0){

        isover = true;
        emit over();
        this->close();
    }

}

void Widget::paintEvent(QPaintEvent *event){

    QPainter painter(this);
    //绘制地图
    painter.drawPixmap(0,m_map.m_map1_posY , m_map.m_map1);
    painter.drawPixmap(0,m_map.m_map2_posY , m_map.m_map2);
    //绘制飞机
    painter.drawPixmap(m_hero.m_X,m_hero.m_Y,m_hero.m_Plane);
    //绘制子弹
    for(int i = 0 ; i < BULLET_NUM;i++)
    {
        //如果子弹状态为非空闲，计算发射位置
        if(!m_hero.m_bullets[i].m_Free)
        {
            painter.drawPixmap(m_hero.m_bullets[i].m_X,m_hero.m_bullets[i].m_Y,m_hero.m_bullets[i].m_Bullet);
        }
    } 
    //绘制敌机
    for(int i = 0 ; i< ENEMY_NUM;i++)
    {
        if(m_enemys[i].m_Free == false)
        {
            painter.drawPixmap(m_enemys[i].m_X,m_enemys[i].m_Y,m_enemys[i].m_enemy);
        }
    }
    //敌机子弹
    for(int i = 0; i < ENEMY_NUM;i++)
    {
        for(int j = 0 ;j < BULLET_NUM ;j++){
            //如果子弹状态为非空闲，计算发射位置
            if(!m_enemys[i].m_bullets[j].m_Free)
            {
                painter.drawPixmap(m_enemys[i].m_bullets[j].m_X,m_enemys[i].m_bullets[j].m_Y,m_enemys[i].m_bullets[j].m_Bullet);
            }
        }
    }
    //绘制爆炸图片
    for(int i = 0 ; i < BOMB_NUM;i++)
    {
        if(m_bombs[i].m_Free == false)
        {
            painter.drawPixmap(m_bombs[i].m_X,m_bombs[i].m_Y,m_bombs[i].m_pixArr[m_bombs[i].m_index]);
        }
    }
}

void Widget::mouseMoveEvent(QMouseEvent *event){
    int x = event->position().x() - m_hero.m_Rect.width()*0.5; //鼠标位置 - 飞机矩形的一半
    int y = event->position().y() - m_hero.m_Rect.height()*0.5;
    //边界检测
    if(x <= 0 )
    {
    x = 0;
    }
    if(x >= GAME_WIDTH - m_hero.m_Rect.width())
    {
    x = GAME_WIDTH - m_hero.m_Rect.width();
    }
    if(y <= 0)
    {
    y = 0;
    }
    if(y >= GAME_HEIGHT - m_hero.m_Rect.height())
    {
    y = GAME_HEIGHT - m_hero.m_Rect.height();
    }
    m_hero.setPosition(x,y);
}

void Widget::enemyToScene()
{

    m_recorder++;
    if(m_recorder < ENEMY_INTERVAL)
    {
        return;
    }
    m_recorder = 0;
    for(int i = 0 ; i< ENEMY_NUM;i++)
    {
        if(m_enemys[i].m_Free)
        {
            //敌机空闲状态改为false
            m_enemys[i].m_Free = false;
            //设置坐标
            m_enemys[i].m_X = rand() % (GAME_WIDTH - m_enemys[i].m_Rect.width());
            m_enemys[i].m_Y = -m_enemys[i].m_Rect.height();
            break;
        }
    }
}

void Widget::collisionDetection()
{
    //遍历所有非空闲的敌机
    for(int i = 0 ;i < ENEMY_NUM;i++)
    {
        if(m_enemys[i].m_Free)
        {
            //空闲飞机 跳转下一次循环
            continue;
        }
        //遍历所有非空闲的子弹
        for(int j = 0 ; j < BULLET_NUM;j++)
        {
            if(m_hero.m_bullets[j].m_Free)
            {
                //空闲子弹 跳转下一次循环
                continue;
            }
            //如果子弹矩形框和敌机矩形框相交，发生碰撞，同时变为空闲状态即可
            if(m_enemys[i].m_Rect.intersects(m_hero.m_bullets[j].m_Rect))
            {
                m_enemys[i].m_Free = true;
                m_hero.m_bullets[j].m_Free = true;
                m_hero.m_score += 0.2;
                //播放爆炸效果
                for(int k = 0 ; k < BOMB_NUM;k++)
                {
                    if(m_bombs[k].m_Free)
                    {
                        //爆炸状态设置为非空闲
                        m_bombs[k].m_Free = false;
                        //更新坐标
                        m_bombs[k].m_X = m_enemys[i].m_X;
                        m_bombs[k].m_Y = m_enemys[i].m_Y;
                        break;
                    }
                }
            }
        }
    }
    //遍历所有非空闲的敌机
    for(int i = 0 ;i < ENEMY_NUM;i++)
    {
        if(m_enemys[i].m_Free)
        {
            //空闲飞机 跳转下一次循环
            continue;
        }
        //遍历所有非空闲的子弹
        for(int j = 0 ; j < BULLET_NUM;j++)
        {
            if(m_enemys[i].m_bullets[j].m_Free)
            {
                //空闲子弹 跳转下一次循环
                continue;
            }
            //如果子弹矩形框和玩家矩形框相交，发生碰撞，同时变为空闲状态即可
            if(m_enemys[i].m_bullets[j].m_Rect.intersects(m_hero.m_Rect))
            {
                m_enemys[i].m_bullets[j].m_Free = true;

                m_hero.m_life -= 1;
            }
         }
    }
}

